//
//  ST_HomeFeedViewController.swift
//  btvideo
//
//  Created by mac on 2020/6/2.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit

class ST_HomeFeedViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = Color.white
        
        
        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: statusBarHeight + 54, left: 0, bottom: 0, right: 0))
        }
        
        scrollView.addSubview(root_view)
        
        let h = ScreenHeight - statusBarHeight - 54
        
        root_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
            make.width.equalTo(ScreenWidth*3)
            make.height.equalTo(h)
        }
        
        self.addChild(recommend_controller)
        self.addChild(last_update_controller)
        self.addChild(my_follow_controller)
        
        
        root_view.addSubview(my_follow_controller.view)
        root_view.addSubview(recommend_controller.view)
        root_view.addSubview(last_update_controller.view)
        
        
        my_follow_controller.view.snp.makeConstraints { (make) in
            make.width.equalTo(ScreenWidth)
            make.height.equalTo(h)
            make.top.bottom.left.equalToSuperview()
        }
        
        recommend_controller.view.snp.makeConstraints { (make) in
            make.width.equalTo(ScreenWidth)
            make.height.equalTo(h)
            make.top.bottom.equalToSuperview()
            make.left.equalTo(my_follow_controller.view.snp.right)
        }
        
        last_update_controller.view.snp.makeConstraints { (make) in
            make.width.equalTo(ScreenWidth)
            make.height.equalTo(h)
            make.top.bottom.right.equalToSuperview()
        }
        
        
        self.view.addSubview(recommend_tab_btn)
        self.view.addSubview(follow_tab_btn)
        self.view.addSubview(sameCityTabBtn)
        
        
        let relativeOffset = 10/375*ScreenWidth
        
        recommend_tab_btn.snp.makeConstraints { (make) in
            make.width.equalTo(76)
            make.height.equalTo(38)
            make.top.equalToSuperview().offset(statusBarHeight+3)
            make.centerX.equalToSuperview()
        }
        
        follow_tab_btn.snp.makeConstraints { (make) in
            make.width.equalTo(76)
            make.height.equalTo(38)
            make.top.equalToSuperview().offset(statusBarHeight+3)
            make.right.equalTo(recommend_tab_btn.snp.left).offset(-relativeOffset)
        }
        
        
        sameCityTabBtn.snp.makeConstraints { (make) in
            make.width.equalTo(80)
            make.height.equalTo(38)
            make.top.equalToSuperview().offset(statusBarHeight+3)
            make.left.equalTo(recommend_tab_btn.snp.right).offset(relativeOffset)
        }
        
        
        let offset:CGFloat = (44-30)/2.0
        
        self.view.addSubview(add_btn)
        add_btn.snp.makeConstraints { (make) in
            make.width.height.equalTo(44)
            make.right.equalToSuperview().offset(-15+offset)
            make.top.equalToSuperview().offset(statusBarHeight+7-offset)
        }
        
        self.view.addSubview(filter_btn)
        filter_btn.snp.makeConstraints { (make) in
            make.width.height.equalTo(44)
            make.left.equalToSuperview().offset(15-offset)
            make.top.equalToSuperview().offset(statusBarHeight+7-offset)
        }
        
        
        
        main_thread_execute(afterDelay: 0.25) {
            self.scrollView.setContentOffset(CGPoint.init(x: ScreenWidth, y: 0), animated: false)
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(member_status_observer), name: Notification.Name.in_pur_status_success, object: nil)
    }
    
    @objc fileprivate func member_status_observer(){
        
    }
    
    fileprivate var normal_selected_btn = false
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        if !normal_selected_btn{
            normal_selected_btn = true
            
            last_btn?.isSelected = false
            
            self.recommend_tab_btn.isSelected = true
            
            last_btn = self.recommend_tab_btn
            
            scrollView.setContentOffset(CGPoint.init(x: 1.cgFloat*ScreenWidth, y: 0), animated: false)
            
        }
        
    }
    
//    /**
//     切换选中之后加载第一次数据
//     */
//    func clickTabWithLazyloadData() {
//
//        self.recommend_controller.refresh_data()
//
//        self.my_follow_controller.refresh_data()
//
//        self.last_update_controller.refresh_data()
//
//    }
    
    
//    MARK:-properties
    fileprivate lazy var scrollView:UIScrollView = {
       let control = UIScrollView()
        control.backgroundColor = Color.white
//        control.isScrollEnabled = false
        control.isPagingEnabled = true
        control.delegate = self
       return control
    }()
    
    
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        control.backgroundColor = Color.white
        return control
    }()
    
    fileprivate lazy var recommend_controller = ST_DailyMomentController()
    
    fileprivate lazy var my_follow_controller = ST_FollowedDailyViewController.shared
    
    fileprivate lazy var last_update_controller = ST_SameCityDailyViewController.shared
   
    fileprivate lazy var btn_selected_image:UIImage = {
           let img = UIImage.init(color: UIColor.init(hexString: "#FEC207")!, size: CGSize.init(width: 1, height: 1))
           return img
       }()
       
       fileprivate lazy var btn_normal_image:UIImage = {
           let img = UIImage.init(color: UIColor.white, size: CGSize.init(width: 1, height: 1))
           return img
       }()

    fileprivate var last_btn:UIButton?
    
    fileprivate var baseTag = 10234
    
    fileprivate lazy var recommend_tab_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setBackgroundImage(btn_selected_image, for: UIControl.State.selected)
        control.setBackgroundImage(btn_normal_image, for: UIControl.State.normal)
        control.cornerRadius = 19
        control.setTitleForAllStates("推荐")
        control.tag = baseTag + 1
        control.setTitleColorForAllStates(UIColor.init(hexString: "#333333")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        control.setTitleColor(UIColor.white, for: UIControl.State.selected)
        control.addTarget(self, action: #selector(click_switch_controller_handle(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
   
    
    
    
    
    fileprivate lazy var follow_tab_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setBackgroundImage(btn_selected_image, for: UIControl.State.selected)
        control.setBackgroundImage(btn_normal_image, for: UIControl.State.normal)
        control.cornerRadius = 19
        control.setTitleForAllStates("喜欢")
        control.tag = baseTag
        control.setTitleColorForAllStates(UIColor.init(hexString: "#333333")!)
        control.setTitleColor(UIColor.white, for: UIControl.State.selected)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(click_switch_controller_handle(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var sameCityTabBtn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setBackgroundImage(btn_selected_image, for: UIControl.State.selected)
        control.setBackgroundImage(btn_normal_image, for: UIControl.State.normal)
        control.cornerRadius = 19
        control.setTitleForAllStates("同城")
        control.tag = baseTag + 2
        control.setTitleColorForAllStates(UIColor.init(hexString: "#333333")!)
        control.setTitleColor(UIColor.white, for: UIControl.State.selected)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(click_switch_controller_handle(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()

    
   
    
    fileprivate lazy var add_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("首页_icon_添加")!)
        control.addTarget(self, action: #selector(push_peculiarity(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var filter_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("首页_icon_筛选")!)
        control.addTarget(self, action: #selector(filter_gender_handle(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
//    MARK: - filter Controller
    fileprivate lazy var homeFilterController:ST_FilterGenderOfDailyViewController = {
            
        let vc = ST_FilterGenderOfDailyViewController()
        vc.overCurrentContext = true
        vc.over_current_context_alpha = 0.5.string
        vc.confirm_closure = {[weak self] gender in
//            接着写
            self?.recommend_controller.filter_remote_data(sex: gender)
            self?.last_update_controller.filter_remote_data(sex: gender)
        }
        
        return vc
    }()
    
    /*
    fileprivate lazy var sameCityFilterController:ST_FilterFeedViewController = {
            
        let vc = ST_FilterFeedViewController()
        vc.overCurrentContext = true
        vc.over_current_context_alpha = 0.5.string
        vc.confirm_closure = {[weak self] gender in
            
            self?.sameCityController.filter_remote_data(sex: gender)
        }
        return vc
    }()
    */
}

//MARK: -Event

extension ST_HomeFeedViewController:UIScrollViewDelegate{
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let  page = (scrollView.contentOffset.x + (0.5 * ScreenWidth)) / ScreenWidth
        
        if  page.int == 0{
            click_switch_controller_handle(sender: self.follow_tab_btn)
        }else if  page.int == 1{
            click_switch_controller_handle(sender: self.recommend_tab_btn)
        }else {
            click_switch_controller_handle(sender: self.sameCityTabBtn)
        }
    }
    
}
extension ST_HomeFeedViewController{
    
    @objc func filter_gender_handle(sender:UIButton){
        
        if self.recommend_tab_btn.isSelected || self.sameCityTabBtn.isSelected{
            GlobalPresent(toController: self.homeFilterController)
        }
        
        /*
        if self.recommend_tab_btn.isSelected{
            
            GlobalPresent(toController: self.homeFilterController)
            
        }
        if self.sameCityTabBtn.isSelected{
            
            GlobalPresent(toController: self.sameCityFilterController)
            
        }*/
        
    }
    
    @objc func push_peculiarity(sender:UIButton){
        
        /* 取消发布日常
        let alert = ST_PushOptionViewController()
        
        alert.overCurrentContext = true
        
        alert.over_current_context_alpha = "0.4"
        
        GlobalPresent(toController: alert)
         */
        
        push_to_push_peculiarity_controller(navigationController: ST_BTLaunchController.shared.navigationController)
    }
    
    @objc func click_switch_controller_handle(sender:UIButton){
        
        if sender.isSelected{
            return
        }
        
        last_btn?.isSelected = false
        
        sender.isSelected = true
        
        last_btn = sender
        
        filter_btn.isHidden = sender == follow_tab_btn
        
        scrollView.setContentOffset(CGPoint.init(x: (sender.tag-self.baseTag).cgFloat*ScreenWidth, y: 0), animated: true)
        
    }
    
}


